import GameEvent from "../../Commons/GameEvent";
import { CharacterType } from "../../Enums/GameEnum";
import { UINames } from "../../Enums/UIEnum";
import AudioManager, { AudioEffectType } from "../../Managers/AudioManager";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import LevelManager from "../Levels/LevelManager";
import TaskManager from "../Tasks/TaskManager";
import CharacterEntity from "./CharacterEntity";
import CharacterTemplate from "./CharacterTemplate";

export enum CharacterEvent {
    UPGRADE_EVENT = "upgrade_event",
    UNLOCKHERO_EVENT = "unlockHero_event"
}

export default class CharacterManager implements IModelManager {

    private static _single: CharacterManager = null;
    private characterMap: Map<number, CharacterTemplate>;
    public characterUnlockGate: any[] = [{ id: 1002, gateId: 3 }, { id: 1003, gateId: 6 }, { id: 1004, gateId: 9 },
    { id: 1005, gateId: 12 }, { id: 2003, gateId: 15 }, { id: 2004, gateId: 18 }, { id: 2005, gateId: 21 }, { id: 3002, gateId: 24 },
    { id: 3003, gateId: 27 }, { id: 3004, gateId: 30 }, { id: 3005, gateId: 33 }];
    public static test: boolean = false;
    public static get single(): CharacterManager {
        if (CharacterManager._single == null) {
            CharacterManager._single = new CharacterManager();
        }
        return CharacterManager._single;
    }

    initConfig(): void {
        var jsonAss: cc.JsonAsset = cc.resources.get("configs/character", cc.JsonAsset)
        this.characterMap = new Map();
        for (const obj of jsonAss.json) {
            var tp = new CharacterTemplate(obj);
            this.characterMap.set(tp.id, tp);
        }
    }
    getUnlockGate(characterId: number): number {
        for (const iterator of this.characterUnlockGate) {
            if (characterId == iterator.id) {
                return iterator.gateId;
            }
        }
        return 0;
    }

    getCharacterTemplate(id: number): CharacterTemplate {
        return this.characterMap.get(id);
    }

    getCharacteEntity(id: number) {
        return SaveManager.single.userdata.getCharacterEntity(id);
    }

    getAllCharacters(type: CharacterType): CharacterTemplate[] {
        var arr: CharacterTemplate[] = [];
        this.characterMap.forEach((tp: CharacterTemplate, key: number, map: Map<number, CharacterTemplate>) => {
            if (tp.characterType == type) {
                arr.push(tp);
            }
        }, this);

        return arr;
    }

    requestUpLevel(id: number) {
        var userdata = SaveManager.single.userdata;
        var characterEy = userdata.getCharacterEntity(id);
        if (characterEy.lv >= 200) {
            UIManager.single.showTips("英雄已经升至满级。");
            return;
        }

        if (!SaveManager.single.isEnoughGold(characterEy.upgradeCost)) {
            UIManager.single.showTips("金币不足。");
            return;
        }
        userdata.gold -= characterEy.upgradeCost;
        characterEy.lv++;

        AudioManager.single.playEffect(AudioEffectType.upgrade);
        if (userdata.gold < 0) {
            userdata.gold = 0;
            console.log("userdata.gold=" + userdata.gold + "，haracterEy.upgradeCost=" + characterEy.upgradeCost);
        }
        TaskManager.single.requestUpdataTaskPregress(104, characterEy.upgradeCost);
        SaveManager.single.saveData();

        GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
        GameEvent.eventNode.emit(CharacterEvent.UPGRADE_EVENT);
        UIManager.single.showTips("升级成功。");
    }

    requestUnlockHero(id: number) {
        var userdata = SaveManager.single.userdata;
        for (const iterator of userdata.heroList) {
            if (iterator.id == id) {
                return;
            }
        }
        var ey: CharacterEntity = new CharacterEntity({ id: id, lv: 1 });
        userdata.heroList.push(ey);
        SaveManager.single.saveData();
        UIManager.single.showTips("解锁<" + ey.characterTp.characterName + ">");
        GameEvent.eventNode.emit(CharacterEvent.UNLOCKHERO_EVENT);
    }

}
